package com.controller;

import com.entity.*;
import com.service.*;
import com.util.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName BanciController
 * @Description 班次模块控制层
 */
@Controller
public class BanciController extends BaseController{

    @Autowired
    private BanciService banciService;

    @Autowired
    private LeibieService leibieService;



    //前台-班次列表页
    @RequestMapping("/banciManaQian")
    public ModelAndView banciManaQian(Banci req, @RequestParam(defaultValue = "1",value = "pageNo")int pageNo){
        ModelAndView mav = new ModelAndView("banciManaQian");
        mav.addObject("actionName","banciManaQian");
        return getModelAndView(req, pageNo, mav, "id");
    }

    //前台-班次图片列表页
    @RequestMapping("/banciManaQiantp")
    public ModelAndView banciManaQiantp(Banci req, @RequestParam(defaultValue = "1",value = "pageNo")int pageNo){
        ModelAndView mav = new ModelAndView("banciManaQiantp");
        return getModelAndView(req, pageNo, mav, "id");
    }

    //后台-班次管理列表页
    @RequestMapping("/banciMana")
    public ModelAndView banciMana(Banci req, @RequestParam(defaultValue = "1",value = "pageNo")int pageNo){
        ModelAndView mav = new ModelAndView("banciMana");
        return getModelAndView(req, pageNo, mav, "id");
    }

    //后台-班次查看列表页
    @RequestMapping("/banciChakan")
    public ModelAndView banciChakan(Banci req, @RequestParam(defaultValue = "1",value = "pageNo")int pageNo){
        ModelAndView mav = new ModelAndView("banciChakan");
        return getModelAndView(req, pageNo, mav, "id");
    }

    //前台-班次按id列排序列表页
    @RequestMapping("/banciManaQianSort")
    public ModelAndView banciManaQianSort(Banci  req, @RequestParam(defaultValue = "1", value = "pageNo") int pageNo) {
        ModelAndView mav = new ModelAndView("banciManaQian");
        mav.addObject("actionName","banciManaQianSort");
        return getModelAndView(req, pageNo, mav, "id");
    }

    //前台-班次详情页
    @GetMapping("/banciDetailQian/{id}")
    @ResponseBody
    public ModelAndView banciDetailQian(@PathVariable Integer id){
        ModelAndView mav = new ModelAndView("banciDetailQian");
        Banci info= banciService.findById(id);
        mav.addObject("banci", info);

        return mav;
    }

    //后台-班次详情页
    @GetMapping("/banciDetail/{id}")
    @ResponseBody
    public ModelAndView banciDetail(@PathVariable Integer id){
        ModelAndView mav = new ModelAndView("banciDetail");
        Banci info= banciService.findById(id);
        mav.addObject("banci", info);
        return mav;
    }

    //获取所有班次数据接口
    @RequestMapping("/banciAll")
    @ResponseBody
    public ApiResponse<Banci> banciAll(){
        List<Banci> banciList=banciService.findAll();
        return ApiResponse.success(banciList);
    }

    //根据条件（字符类型模糊匹配查询）获取班次数据接口
    @RequestMapping("/banciSearch")
    @ResponseBody
    public ApiResponse<Banci> banciSearch(Banci req){
        List<Banci> banciList=banciService.find(req);
        return ApiResponse.success(banciList);
    }

    //根据条件（字符类型完全匹配查询）获取班次数据接口
    @RequestMapping("/banciSearchByEqualTo")
    @ResponseBody
    public ApiResponse<Banci> banciSearchByEqualTo(Banci req){
        List<Banci> banciList=banciService.findByEqualTo(req);
        return ApiResponse.success(banciList);
    }

    //班次添加或修改接口
    @PostMapping("/banciAddOrUpdate")
    @ResponseBody
    public ApiResponse<Integer> banciAddOrUpdate(@RequestBody Banci req, HttpSession session, Boolean isUPdateSession){



        int result = banciService.saveOrUpdate(req);


        return ApiResponse.success(result);
    }

    //班次修改单独一个字段接口
    @PostMapping("/banciSet")
    @ResponseBody
    public ApiResponse<Integer> banciSet(Banci req){
        int result = banciService.saveOrUpdate(req);
        return ApiResponse.success(result);
    }



    //班次删除接口
    @RequestMapping("/banciDel/{id}")
    @ResponseBody
    public ApiResponse<Integer> banciDel(@PathVariable Integer id){
        int result = banciService.deleteById(id);
        return ApiResponse.success(result);
    }

    //后台-班次修改页
    @RequestMapping("/banciGet/{id}")
    @ResponseBody
    public ModelAndView banciGet(@PathVariable Integer id){
        ModelAndView mav = new ModelAndView("banciSet");
        Banci info = banciService.findById(id);
        mav.addObject("banci", info);
        return mav;
    }





    //前台-类别添加班次页面
    @GetMapping("/leibiebanciAddQian/{leibieid}")
    @ResponseBody
    public ModelAndView leibiebanciAddQian(@PathVariable Integer leibieid){
        ModelAndView mav = getLeibieBanciAddModelAndView(leibieid,"leibiebanciAddQian");
        return mav;
    }

    //后台-类别添加班次页面
    @GetMapping("/leibiebanciAdd/{leibieid}")
    @ResponseBody
    public ModelAndView leibiebanciAdd(@PathVariable Integer leibieid){
        ModelAndView mav = getLeibieBanciAddModelAndView(leibieid,"leibiebanciAdd");
        return mav;
    }

    //后台-类别班次查看页面
    @RequestMapping("/leibiebanciGetByleibieid/{leibieid}")
    @ResponseBody
    public ModelAndView leibiebanciGetByleibieid(Banci req, @PathVariable Integer leibieid, @RequestParam(defaultValue = "1",value = "pageNo")int pageNo){
        ModelAndView mav = new ModelAndView("leibiebanciChakan");
        req.setLeibieid(leibieid);
        return getModelAndView(req, pageNo, mav, "id");
    }

    //前台-类别班次查看页面
    @RequestMapping("/leibiebanciGetByleibieidQian/{leibieid}")
    @ResponseBody
    public ModelAndView leibiebanciGetByleibieidQian(Banci req, @PathVariable Integer leibieid, @RequestParam(defaultValue = "1",value = "pageNo")int pageNo){
        ModelAndView mav = new ModelAndView("banciManaQian");
            mav.addObject("actionName","banciManaQian");
        req.setLeibieid(leibieid);
        return getModelAndView(req, pageNo, mav, "id");
    }


    //类别班次数据封装
    private ModelAndView getLeibieBanciAddModelAndView(Integer leibieid, String viewName) {
        ModelAndView mav = new ModelAndView(viewName);
        Leibie leibie=leibieService.findById(leibieid);
        mav.addObject("leibie", leibie);

        return mav;
    }



    //班次模糊匹配查询分页数据封装
    private ModelAndView getModelAndView(Banci req, int pageNo, ModelAndView mav, String sortProperty) {
        PageWrap<Banci> pageWrap=new PageWrap<Banci>();
        pageWrap.setModel(req);
        pageWrap.setPageNum(pageNo);
        //设置每页最多几条数据，默认为8条
        //pageWrap.setPageSize(8);
        PageWrap.SortData sortData=new PageWrap.SortData();
        sortData.setDirection("DESC");
        sortData.setProperty(sortProperty);
        List<PageWrap.SortData> sorts = new ArrayList<>();
        sorts.add(sortData);
        pageWrap.setSorts(sorts);
        PageData<Banci> pageInfo= banciService.findPage(pageWrap);
        mav.addObject("pageInfo", pageInfo);


        mav.addObject("req", req);
        List<Leibie> leibieList=leibieService.findAll();
        mav.addObject("leibieList", leibieList);

        return mav;
    }

    //班次完全匹配查询分页数据封装
    private ModelAndView getModelAndViewByEqualTo(Banci req, int pageNo, ModelAndView mav, String sortProperty) {
        PageWrap<Banci> pageWrap=new PageWrap<Banci>();
        pageWrap.setModel(req);
        pageWrap.setPageNum(pageNo);
        //设置每页最多几条数据，默认为8条
        //pageWrap.setPageSize(8);
        PageWrap.SortData sortData=new PageWrap.SortData();
        sortData.setDirection("DESC");
        sortData.setProperty(sortProperty);
        List<PageWrap.SortData> sorts = new ArrayList<>();
        sorts.add(sortData);
        pageWrap.setSorts(sorts);
        PageData<Banci> pageInfo= banciService.findPageByEqualTo(pageWrap);
        mav.addObject("pageInfo", pageInfo);


        mav.addObject("req", req);
        List<Leibie> leibieList=leibieService.findAll();
        mav.addObject("leibieList", leibieList);

        return mav;
        }
}
